<?php

namespace app\admin\controller;

use app\service\MsgService;
use think\Controller;
use app\common\model;
use app\common\model\Menu;

class Base extends Controller
{
    public $manager;
    public $managerModel;

    public function __construct()
    {
//        header("Access-Control-Allow-Origin:*");
//        header("Access-Control-Allow-Methods:GET,POST");
//        header("Access-Control-Allow-Headers:x-requested-with,content-type");
        header('Access-Control-Allow-Origin: *'); // 允许所有域访问
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header('Access-Control-Allow-Headers: X-Requested-With, Content-Type, Accept, Origin, Authorization');
        header('Access-Control-Allow-Credentials: true'); // 允许携带凭证

        $this->setConfig();
    }

    protected function setConfig()
    {
        $configModel = new model\Config;
        config($configModel->getConfig());
    }

    /**
     * 返回
     * @param $code
     * @param $message
     * @param null $data
     * @param null $dataName
     */
    protected function ajax($code, $message, $data = null, $dataName = null)
    {
        if ($data != null && $data !== 0) {
            if ($dataName == null) {
                echo json_encode(array('code' => $code, 'msg' => $message, 'data' => $data));
                exit();
            } else {
                echo json_encode(array('code' => $code, 'msg' => $message, $dataName => $data));
                exit();
            }
        } else {
            echo json_encode(array('code' => $code, 'msg' => $message, 'data' => array()));
            exit();
        }
    }
}
